/**
 * @file app_uart.c
 * @brief ����ǰ��֪ͨ��ˣ�DMA������ȫ����USART���лص�����ʵ��
 * @author CYH
 * @date 2025��5��12��
 * @version V1.0
 */
#include "app_uart.h"
#include "app_uart_sever.h"
#include "communication_interface.h"
#include "mid_circular_buffer.h"
/************************** ȫ�ֱ��� ****************************/
uint8_t 	  			g_uart1_com_num;		// ����ӿں�
uint8_t					g_uart_rx_flag;			// �ɿ����ж���λ
#define UART_ISR_SEND 0xA1A2A3A4				// �����жϵ���Ϣ
#define UART_APP_SEND 0xB1B2B3B4				// ���͸���˵���Ϣ
#define UART1_CB_SIZE (30)						// ����1����buf��С
/************************** ȫ�ֱ��� ****************************/

/**
 * @brief  ����1ǰ�˺���
 * @param  ��
 * @Attention ֪ͨ����߳�
 * @return ��
 */
void uart_task(void)
{
	static uint8_t uart_init_flag = 0;
	
	// ��ʼ��
	if(0 == uart_init_flag)
	{
		g_uart1_com_num = com_handle_init(UART, 1, UART1_CB_SIZE);
		uart_init_flag = 1;
	}
	
	// δ���ܵ���Ϣ
	if(g_uart_rx_flag == 0) return;
	g_uart_rx_flag = 0;
	
	// ��ȡ���λ�����ָ��
	CircularBuffer_t *uart_cb = get_cb(g_uart1_com_num);
	
	// ��������
#if 1// ���ݽ���
	Analytic_function(uart_cb, g_uart1_com_num);
#endif
#if 0// test app_uart
	uint8_t temp_dat;
	while(EMPTY_OK == CircularBuffer_IsEmpty(uart_cb))
	{
		CircularBuffer_Get(uart_cb, &temp_dat);
		my_printf(&huart1, "data is :%c\n" ,(char)temp_dat);
	}
#endif
}

/**
 * @brief  DMA�����ص�����
 * @param  size : ���յ�����������
 * @Attention ������stm32fxx_hal_uart.c(3136)��
 *			  ������stm32fxx_hal_uart.c(3155)��
 * @return ��
 */
void dma_half_callback(uint32_t size)
{
	uint32_t head = 0;
	uint32_t add  = 0;
	CircularBuffer_Get_head(get_cb(g_uart1_com_num), &head);
	add = (UART1_CB_SIZE/2)-(head%(UART1_CB_SIZE/2));
	CircularBuffer_Add_head(get_cb(g_uart1_com_num), add);
	
//	printf("dma_half_callback\n");
}

/**
 * @brief  DMAȫ���ص�����
 * @param  size : ���յ�����������
 * @Attention ������stm32fxx_hal_uart.c(3072)��
 *			  ������stm32fxx_hal_uart.c(3114)��
 * @return ��
 */
void dma_All_callback(uint32_t size)
{
    uint32_t head = 0;
    uint32_t add  = 0;

    CircularBuffer_Get_head(get_cb(g_uart1_com_num), &head);
    add = UART1_CB_SIZE - head % UART1_CB_SIZE;
    CircularBuffer_Add_head(get_cb(g_uart1_com_num), add);

    g_uart_rx_flag = 1;
//    my_printf(&huart1, "enter dma full\n");
}

/**
 * @brief  ���ڿ����жϻص�����
 * @param  size : ���յ�����������
 * @Attention ������stm32fxx_hal_uart.c(2355)��
 *			  ������stm32fxx_hal_uart.c(2536)��
 *			  ������stm32fxx_hal_uart.c(2594)��
 * @return ��
 */
void uart_idle_callback(uint32_t size)
{
	uint32_t head = 0;
	uint32_t add  = 0;
	CircularBuffer_Get_head(get_cb(g_uart1_com_num), &head);
	add = size-head%UART1_CB_SIZE;
	CircularBuffer_Add_head(get_cb(g_uart1_com_num), add);
	
	g_uart_rx_flag = 1;
//	my_printf(&huart1, "enter idle\n" );
}

